package com.warmlight.voicepacket.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.util.DisplayMetrics;
import android.util.TypedValue;

import com.facebook.drawee.view.SimpleDraweeView;
import com.warmlight.voicepacket.BuildConfig;
import com.warmlight.voicepacket.MApplication;

public class UIUtils {
	public static DisplayMetrics displayMetrics;

	public static Context getContext() {
		return MApplication.getInstance();
	}

	/** dip转换px */
	public static int dip2px(int dip) {
		final float scale = getContext().getResources().getDisplayMetrics().density;
		return (int) (dip * scale + 0.5f);
	}

	/** pxz转换dip */
	public static int px2dip(int px) {
		final float scale = getContext().getResources().getDisplayMetrics().density;
		return (int) (px / scale + 0.5f);
	}

	/** 转化为指定单元的数据 */
	public static int applyDimension(int unit, float value) {
		if (displayMetrics != null) {
			TypedValue.applyDimension(unit, value, displayMetrics);
		}
		return 0;
	}

	public static int getDip(Context context, float param) {
		return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
				param, context.getResources().getDisplayMetrics());
	}

	public static void loadImage(SimpleDraweeView simpleDraweeView, String url) {
		try {
			Uri uri = Uri.parse(url);
			simpleDraweeView.setImageURI(uri);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * dp/sp转化为px值
	 * @param dp
	 * @return
	 */
	public static float getPixels(int dp) {
		final float scale = UIUtils.getContext().getResources()
				.getDisplayMetrics().density;
		int px = (int) (dp * scale + 0.5f);
		return px;
	}

	public static float px2dp(Context context, float pxVal) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (pxVal / scale);
	}

	/** 获取资源 */
	public static Resources getResources() {
		return getContext().getResources();
	}

	/** 获取文字 */
	public static String getString(int resId) {
		return getResources().getString(resId);
	}

	/** 获取文字数组 */
	public static String[] getStringArray(int resId) {
		return getResources().getStringArray(resId);
	}

	/** 获取dimen */
	public static int getDimens(int resId) {
		return getResources().getDimensionPixelSize(resId);
	}

	/** 获取drawable */
	public static Drawable getDrawable(int resId) {
		return getResources().getDrawable(resId);
	}

	/** 获取颜色 */
	public static int getColor(int resId) {
		return getResources().getColor(resId);
	}

	public static int getPackageCode() {
		PackageManager manager = getContext().getPackageManager();
		int code = 0;
		try {
			PackageInfo info = manager.getPackageInfo(getContext().getPackageName(), 0);
			code = info.versionCode;
		} catch (PackageManager.NameNotFoundException e) {
			e.printStackTrace();
		}
		return code;
	}

	public static String getPackageName() {
		PackageManager manager = getContext().getPackageManager();
		String code = "";
		try {
			PackageInfo info = manager.getPackageInfo(getContext().getPackageName(), 0);
			code = info.versionName;
		} catch (PackageManager.NameNotFoundException e) {
			e.printStackTrace();
			code = BuildConfig.VERSION_NAME;
		}
		return code;
	}

}
